package cn.hdu.liang.holding;

import cn.hdu.liang.typeinfo.pets.Pet;
import cn.hdu.liang.typeinfo.pets.Pets;

import java.util.Iterator;
import java.util.List;

public class SimpleIteration {
    public static void main(String[] args) {
        List<Pet> pets = Pets.arrayList(12);
        System.out.println(pets);
        System.out.println();

        //迭代器语法
        Iterator<Pet> it = pets.iterator();
        while(it.hasNext()) {
            Pet p = it.next();
            System.out.print(p.id() + ":" + p + " ");
        }
        System.out.println("\n");

        // foreach语法
        for(Pet p : pets)
            System.out.print(p.id() + ":" + p + " ");
        System.out.println("\n");

        //迭代器也可以删除
        it = pets.iterator();
        for(int i = 0; i < 6; i++) {
            it.next();
            it.remove();
        }
        System.out.println(pets);
    }
}
